package cn.lingyangwl.agile.system.manager;

import cn.lingyangwl.agile.common.core.exception.enums.GlobalErrorEnum;
import cn.lingyangwl.agile.system.mapper.SysMenuMapper;
import cn.lingyangwl.agile.system.mapper.SysMenuDataRuleMapper;
import cn.lingyangwl.agile.system.mapper.SysRoleMenuRelMapper;
import cn.lingyangwl.agile.system.model.entity.SysMenu;
import cn.lingyangwl.agile.system.model.entity.SysMenuRule;
import cn.lingyangwl.agile.system.model.entity.SysRoleMenu;
import cn.lingyangwl.agile.system.model.rqrs.menu.MenuListReq;
import cn.lingyangwl.framework.tool.core.CollectionUtils;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
public class SysMenuManager extends ServiceImpl<SysMenuMapper, SysMenu>
        implements IService<SysMenu> {
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private SysMenuDataRuleMapper sysMenuDataRuleMapper;
    @Resource
    private SysRoleMenuRelMapper sysRoleMenuRelMapper;


    public List<SysMenu> listMenuData(MenuListReq req) {
        LambdaQueryWrapper<SysMenu> wrapper = SysMenu.lqw()
                .eq(StringUtils.isNotEmpty(req.getStatus()), SysMenu::getStatus, req.getStatus())
                .eq(Objects.nonNull(req.getSceneId()), SysMenu::getSceneId, req.getSceneId())
                .orderByAsc(Arrays.asList(SysMenu::getParentId, SysMenu::getSortNum));
        return sysMenuMapper.selectList(wrapper);
    }

    public Collection<SysMenu> listMenuDataByIds(List<Long> menuIds) {
        if (CollectionUtils.isEmpty(menuIds)) {
            return Collections.emptyList();
        }
        return this.listByIds(menuIds);
    }

    public void deleteMenu() {
        LambdaQueryWrapper<SysMenu> lqw = SysMenu.lqw();

        // 删除菜单
        List<SysMenu> menuList = this.list(lqw);
        List<Long> menuIds = menuList.stream().map(SysMenu::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(menuIds)) {
            return;
        }
        if (!this.remove(lqw)) {
            throw new BizException(GlobalErrorEnum.OPERATION_FAIL_DELETE.getCode(), "删除菜单失败");
        }

        // 删除菜单关联
        int deleteMenuCount = sysMenuDataRuleMapper.delete(SysMenuRule.lqw().eq(SysMenuRule::getMenuId, menuList));
        log.info("删除的菜单数量: {}", deleteMenuCount);

        // 删除角色和菜单
        sysRoleMenuRelMapper.delete(SysRoleMenu.lqw().in(SysRoleMenu::getMenuId, menuIds));
    }

    public List<SysMenu> listByParentId(Long parentId) {
        return this.list(SysMenu.lqw().eq(SysMenu::getParentId, parentId));
    }

    public void updatePerm(String oldAction, String newAction) {
        LambdaUpdateWrapper<SysMenu> luw = SysMenu.luw().eq(SysMenu::getPerms, oldAction)
                .set(SysMenu::getPerms, newAction);
        this.update(luw);
    }

    public List<SysMenu> listByCopyId(Long copyId) {
        LambdaQueryWrapper<SysMenu> lqw = SysMenu.lqw().eq(SysMenu::getCopyId, copyId);
        return this.list(lqw);
    }
}
